package com.treetao.union.mall.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.treetao.union.core.tencent.microapp.TencentMicroappUserService;
import com.treetao.union.core.tencent.microapp.option.MicroappOption;
import com.treetao.union.core.util.JwtTokenUtil;
import com.treetao.union.db.model.vo.RestResponse;
import com.treetao.union.mall.api.base.BaseApiController;
import com.treetao.union.mall.api.service.TencentDbService;
import com.treetao.union.mall.api.vo.req.WxwaLoginRequestParam;
import com.treetao.union.mall.api.vo.resp.WxLoginResp;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;

/**
 * 微信小程序相关
 * @author Dev.M
 *
 */
@RestController
@RequestMapping("/tencent")
public class WxMicroappController extends BaseApiController {
	
	@Autowired
	private TencentMicroappUserService tencentMicroappUserService;
	@Autowired
	private TencentDbService tencentDbService;
	
	@PostMapping("/microapp/login")
	public RestResponse login(@RequestBody WxwaLoginRequestParam param) {
		MicroappOption option = initTencentWxConfig();
		tencentMicroappUserService.config(option);
		WxMaJscode2SessionResult jscode2SessionResult = tencentMicroappUserService.code2session(param.getCode());
		if(jscode2SessionResult!=null) {
			String openid = jscode2SessionResult.getOpenid();
			Long wxuid = tencentDbService.checkAndUpdateWxUser(param.getAppid(), openid,jscode2SessionResult.getSessionKey());
			String token = JwtTokenUtil.generateToken(openid+"@"+String.valueOf(wxuid));
			WxLoginResp resp = new WxLoginResp();
			resp.setOpenid(openid);
			resp.setToken(token);
			return RestResponse.success(resp);
		}
		return RestResponse.failure("登录 失败");
	}
	
	@PostMapping("/microapp/setuserinfo")
	public RestResponse auth(@RequestBody WxwaLoginRequestParam param) {
		MicroappOption option = initTencentWxConfig();
		tencentMicroappUserService.config(option);
		String openid = getOpenidFromToken();
		String sessionkey = tencentDbService.getUserSessionKey(param.getAppid(), openid);
		WxMaUserInfo wxMaUserInfo = tencentMicroappUserService.getUserInfo(sessionkey, param.getSignature(), param.getRawData(), param.getEncryptedData(), param.getIv());
		boolean flag = tencentDbService.saveUser(param.getAppid(), wxMaUserInfo);
		if(flag) {
			return RestResponse.success(tencentDbService.getUserInfo(getWxuidFromToken()));
		}
		return RestResponse.failure("获取信息失败");
	}
	
	@PostMapping("/microapp/getphone")
	public RestResponse getphone(@RequestBody WxwaLoginRequestParam param) {
		MicroappOption option = initTencentWxConfig();
		tencentMicroappUserService.config( option);
		String openid = getOpenidFromToken();
		String sessionkey = tencentDbService.getUserSessionKey(param.getAppid(), openid);
		WxMaPhoneNumberInfo info = tencentMicroappUserService.getPhone( sessionkey, param.getEncryptedData(), param.getIv());
		return RestResponse.success().setData(info.getPurePhoneNumber());
	}
	
	@GetMapping("/microapp/getuserinfo")
	public RestResponse getuserinfo() {
		Long userId = getWxuidFromToken();
		return RestResponse.success(tencentDbService.getUserInfo(userId));
	}
}
